package com.huika.dealontop.control.timelimit.fragment;

import android.app.usage.UsageEvents;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseFra;
import com.huika.dealontop.control.homepage.database.DindanDbHelper;
import com.huika.dealontop.control.login.activity.LoginActivity;
import com.huika.dealontop.control.main.MainActivity;
import com.huika.dealontop.control.myinfo.activity.ModifiedEmailActivity;
import com.huika.dealontop.control.myinfo.activity.ModifiedMobilePhoneActivity;
import com.huika.dealontop.control.myinfo.adapter.ShoppingCartAdapter;
import com.huika.dealontop.control.pay.activity.ChoosePayModesActivity;
import com.huika.dealontop.control.pay.utils.BasePay;
import com.huika.dealontop.control.shops.activity.ProductDetailsActivity1;
import com.huika.dealontop.control.shops.activity.UseBonusActivity;
import com.huika.dealontop.control.timelimit.activity.ShoppingCartAtc;
import com.huika.dealontop.support.domain.SelectGoods;
import com.huika.dealontop.support.domain.SoppingCartBean;
import com.huika.dealontop.support.domain.SubmitBean;
import com.huika.dealontop.support.domain.UserModel;
import com.huika.dealontop.support.event.CallMainPagerChangeEvent;
import com.huika.dealontop.support.event.SelectLanguageEvent;
import com.huika.dealontop.support.event.SelectShoppingCarEvent;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.LoginUtils;
import com.huika.dealontop.utils.SelectLanguageUtils;
import com.huika.dealontop.views.CommonTitleRightWindow;
import com.huika.dealontop.views.PromptOperatDialog;
import com.huika.dealontop.views.PullToSwipeMenuListView;
import com.huika.dealontop.views.TitleBarHelper;
import com.huika.dealontop.views.swipemenulistview.SwipeMenu;
import com.huika.dealontop.views.swipemenulistview.SwipeMenuCreator;
import com.huika.dealontop.views.swipemenulistview.SwipeMenuItem;
import com.huika.dealontop.views.swipemenulistview.SwipeMenuListView;
import com.huika.huikalib.utils.MoneyShowTool;
import com.huika.huikalib.utils.StringUtils;
import com.mob.tools.gui.Scrollable;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;

public class ShoppingCartFragment extends BaseFra implements PullToRefreshBase.OnRefreshListener2<SwipeMenuListView> {

    public static String PRICE_COUPON = "price_coupon";
    public static String INPUT_COUPON_CODE = "input_coupon_code";
    public static String TYPETITLE = "typeTitle";
    private int typeTitle = 0;
    private CommonTitleRightWindow moreWindow;
    private ImageView title_right_iv;

    //未登录和没有数据时候的覆盖
    private LinearLayout shopping_null;
    private ImageView shopping_null_img;
    private TextView shopping_null_megss;
    private Button shopping_null_btn;
    private ImageView iv_back_to_top;

    private PullToSwipeMenuListView ptrLv;
    private ShoppingCartAdapter mAdapter;
    private List<SoppingCartBean> mAppList = new ArrayList<SoppingCartBean>();

    private ImageView shopping_cart_all;
    private TextView shopping_cart_total_prices;
    private TextView shopping_cart_marked;
    private LinearLayout shopping_cart_discount;
    private Button btn_submit_order;
    private LinearLayout has_select_coupon_loy;
    private LinearLayout has_select_you_loy;
    private TextView rb_price_tv;

    //修改邮箱或手机
    private LinearLayout login_loy;
    private LinearLayout login_loy_phone;
    private TextView cell_phone_num_tv;
    private LinearLayout login_loy_email;
    private TextView mail_num_tv;
    //
    private int loy_chox = 0;
    private ImageView login_loy_phone_chox;
    private TextView login_loy_phone_modification;
    private ImageView login_loy_email_chox;
    private TextView login_loy_email_modification;

    private HashMap<String, String> datas = new HashMap<String, String>();
    private String hasSelectCouponNum;
    public float priea = 0f;

    private boolean falseone = true;//判断登录以后或者购物车修改需要掉一次接口

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View root = inflater.inflate(R.layout.shopping_cart_activity, container, false);
        return root;
    }

    @Override
    protected void initWidget(View parentView) {
        super.initWidget(parentView);
        TitleBarHelper titlebar =  new TitleBarHelper(getActivity(), R.string.common_empty, R.string.common_empty, R.string.shopping_cart);
        titlebar.setRightImg(new DindanDbHelper().getUnUsedableCountNum() > 0 ? R.mipmap.common_more_has_message_icon : R.mipmap.common_more_icon);
        if (typeTitle == 0) {
            titlebar.setLeftMsg(-1);
            titlebar.hideRightImg();
        }
        title_right_iv = (ImageView) parentView.findViewById(R.id.title_right_iv);
        iv_back_to_top = (ImageView) parentView.findViewById(R.id.iv_back_to_top);
        shopping_null = (LinearLayout) parentView.findViewById(R.id.shopping_null);
        shopping_null_img = (ImageView) parentView.findViewById(R.id.shopping_null_img);
        shopping_null_megss = (TextView) parentView.findViewById(R.id.shopping_null_megss);
        shopping_null_btn = (Button) parentView.findViewById(R.id.shopping_null_btn);
        ptrLv = (PullToSwipeMenuListView) parentView.findViewById(R.id.ptrlv_ilearnthe);
        ptrLv.setMode(PullToRefreshBase.Mode.DISABLED);
        ptrLv.setOnRefreshListener(this);
        getBelow();//添加下面布局

        mAdapter = new ShoppingCartAdapter(getActivity());
        mAdapter.setAct(this);
        ptrLv.setAdapter(mAdapter);

        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getActivity().getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xFC,
                        0x70, 0x2B)));
                deleteItem.setWidth(dp2px(105));
                deleteItem.setTitle(getActivity().getResources().getString(R.string.shopping_delete));
                deleteItem.setTitleSize(20);
                deleteItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(deleteItem);
            }
        };
        ptrLv.setMenuCreator(creator);

        ptrLv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        double itemnum = mAppList.get(position).HKPrice * mAppList.get(position).Quantity;
                        if ((tempCoup > 0 || couponDiscountPrice > 0) && (priea - itemnum) < 99) {
                            GetViewDialog(1,"3",  false, mAppList.get(position).ShoppingCartId + "", 0, 0);
                        }else if ((tempCoup > 0 ) && (priea - itemnum) <= tempCoup){
                            GetViewDialog(2,"3",  false, mAppList.get(position).ShoppingCartId + "", 0, 0);
                        }else{
                            getDeleteData(mAppList.get(position).ShoppingCartId);
                        }
                        break;
                }
            }
        });
        setOnclik();
        EventBus.getDefault().register(this);
    }

    private void setOnclik(){
        ptrLv.setOnScrollListener(new MyOnScrollListener());
        ptrLv.setOnItemClickListener(new MyItemClickListener());
        title_right_iv.setOnClickListener(this);
        iv_back_to_top.setOnClickListener(this);
        shopping_null.setOnClickListener(this);
        shopping_null_btn.setOnClickListener(this);
        shopping_cart_all.setOnClickListener(this);
        has_select_coupon_loy.setOnClickListener(this);
        has_select_you_loy.setOnClickListener(this);
        btn_submit_order.setOnClickListener(this);
        login_loy_phone.setOnClickListener(this);
        login_loy_email.setOnClickListener(this);
        login_loy_phone_modification.setOnClickListener(this);
        login_loy_email_modification.setOnClickListener(this);
    }

    class MyItemClickListener implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), ProductDetailsActivity1.class);
            intent.putExtra(ProductDetailsActivity1.PRODUCT_ID, mAppList.get(position - 1).ProductId + "");
            intent.putExtra(ProductDetailsActivity1.PRODUCT_IMG, mAppList.get(position - 1).Pic);
            intent.putExtra(ProductDetailsActivity1.SKU_ID, mAppList.get(position - 1).SkuProductId);
            showActivity(getActivity(), intent);
        }
    }

    private void getBelow(){
        ListView list = ptrLv.getlv();
        View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.footerview_soppingcart,null);
        shopping_cart_all = (ImageView) footerView.findViewById(R.id.shopping_cart_all);
        shopping_cart_total_prices = (TextView) footerView.findViewById(R.id.shopping_cart_total_prices);
        shopping_cart_marked = (TextView) footerView.findViewById(R.id.shopping_cart_marked);
        shopping_cart_discount = (LinearLayout) footerView.findViewById(R.id.shopping_cart_discount);
        btn_submit_order = (Button) footerView.findViewById(R.id.btn_submit_order);
        has_select_coupon_loy = (LinearLayout) footerView.findViewById(R.id.has_select_coupon_loy);
        has_select_you_loy = (LinearLayout) footerView.findViewById(R.id.has_select_you_loy);
        rb_price_tv = (TextView) footerView.findViewById(R.id.rb_price_tv);

        login_loy = (LinearLayout) footerView.findViewById(R.id.login_loy);
        login_loy_phone = (LinearLayout) footerView.findViewById(R.id.login_loy_phone);
        cell_phone_num_tv = (TextView) footerView.findViewById(R.id.cell_phone_num_tv);
        login_loy_email = (LinearLayout) footerView.findViewById(R.id.login_loy_email);
        mail_num_tv = (TextView) footerView.findViewById(R.id.mail_num_tv);

        login_loy_phone_chox = (ImageView) footerView.findViewById(R.id.login_loy_phone_chox);
        login_loy_phone_modification = (TextView) footerView.findViewById(R.id.login_loy_phone_modification);
        login_loy_email_chox = (ImageView) footerView.findViewById(R.id.login_loy_email_chox);
        login_loy_email_modification = (TextView) footerView.findViewById(R.id.login_loy_email_modification);

        list.addFooterView(footerView);
    }
    private void getNet(){
        getSoppingCart();
    }

    @Override
    protected void initData() {
        super.initData();
        typeTitle = getActivity().getIntent().getIntExtra(TYPETITLE,0);
    }

    @Override
    public void onResume() {
        super.onResume();
        login_loy.setVisibility(View.GONE);
        login_loy_phone.setVisibility(View.GONE);
        login_loy_email.setVisibility(View.GONE);
        UserModel usermodel = GlobalApp.getInstance().getUserModel();
        if (usermodel != null) {
            if (!StringUtils.isEmpty(usermodel.getPhone())) {
                loy_chox = 1;
                login_loy.setVisibility(View.VISIBLE);
                login_loy_phone.setVisibility(View.VISIBLE);
                cell_phone_num_tv.setText(getString(R.string.mobile_phone) + " " +StringUtils.dealStrPhone(usermodel.getPhone()));
                login_loy_phone_chox.setImageResource(R.mipmap.common_select_choise_icon);
                login_loy_email_chox.setImageResource(R.mipmap.common_unselect_choise_icon);
            }
            if (!StringUtils.isEmpty(usermodel.getEmail())) {
                login_loy.setVisibility(View.VISIBLE);
                login_loy_email.setVisibility(View.VISIBLE);
                mail_num_tv.setText(getString(R.string.register_mail) + " " +StringUtils.dealStrEmail(usermodel.getEmail()));
                if (loy_chox != 1) {
                    loy_chox = 2;
                    login_loy_phone_chox.setImageResource(R.mipmap.common_unselect_choise_icon);
                    login_loy_email_chox.setImageResource(R.mipmap.common_select_choise_icon);
                }
            }
        }

        if (LoginUtils.isLogin()) {//登录以后只请求一次
            if (falseone) {
                falseone = false;
                getNet();
                if (mAppList.size()==0) {
                    hideView();
                    startOverallLoading(ptrLv);//第一次加载动画
                }
            }
        }else{
            falseone = true;
            mAppList.clear();
            loginView();
        }
    }

    private void loginView(){
        shopping_null.setVisibility(View.VISIBLE);
        shopping_null_img.setImageResource(R.mipmap.shopping_null_login_btn);
        shopping_null_megss.setText(getString(R.string.shopping_null_login));
        shopping_null_btn.setText(getString(R.string.title_login));
    }
    private void shoppingView(){
        shopping_null.setVisibility(View.VISIBLE);
        shopping_null_img.setImageResource(R.mipmap.shopping_null_btn);
        shopping_null_megss.setText(getString(R.string.shopping_null_megss));
        shopping_null_btn.setText(getString(R.string.shopping_null_btn));
    }
    private void hideView(){
        shopping_null.setVisibility(View.GONE);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.shopping_cart_all://是否全选
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < mAppList.size(); i++) {
                    if (getboolSelect()) {
                        if (mAppList.get(i).Status != 5) {
                            if (sb.length() == 0) {
                                sb.append(mAppList.get(i).ShoppingCartId+"");
                            }else{
                                sb.append(","+mAppList.get(i).ShoppingCartId);
                            }
                        }
                    }else{
                        if (mAppList.get(i).Status != 5 && mAppList.get(i).IsSelected == 0) {
                            if (sb.length() == 0) {
                                sb.append(mAppList.get(i).ShoppingCartId+"");
                            }else{
                                sb.append(","+mAppList.get(i).ShoppingCartId);
                            }
                        }
                    }
                }

                if ((tempCoup == 0 && couponDiscountPrice == 0)) {
                    getSelectGoods(!getboolSelect(), sb.toString());
                }else{
                    GetViewDialog(1,"1",!getboolSelect(), sb.toString(),0,0);
                }
                break;
            case R.id.has_select_coupon_loy://使用优惠券
            case R.id.has_select_you_loy://使用优惠券
                if(!LoginUtils.isLogin()){
                    showActivity(getActivity(),LoginActivity.class);
                    return;
                }
                Intent intent = new Intent(getActivity(), UseBonusActivity.class);
                intent.putExtra(UseBonusActivity.HASSELECTCOUPONNUM, hasSelectCouponNum);
                startActivityForResult(intent, 2000);
                break;
            case R.id.btn_submit_order://提交订单
                getSubmitOrder();
                break;
            case R.id.login_loy_phone_modification://修改手机
                showActivity(getActivity(), ModifiedMobilePhoneActivity.class);
                break;
            case R.id.login_loy_email_modification://修改邮箱
                showActivity(getActivity(), ModifiedEmailActivity.class);
                break;
            case R.id.login_loy_phone://修改手机
                loy_chox = 1;
                login_loy_phone_chox.setImageResource(R.mipmap.common_select_choise_icon);
                login_loy_email_chox.setImageResource(R.mipmap.common_unselect_choise_icon);
                break;
            case R.id.login_loy_email://修改邮箱
                loy_chox = 2;
                login_loy_phone_chox.setImageResource(R.mipmap.common_unselect_choise_icon);
                login_loy_email_chox.setImageResource(R.mipmap.common_select_choise_icon);
                break;
            case R.id.shopping_null_btn://登录或跳转赛选
                if (shopping_null_btn.getText().toString().equals(getString(R.string.title_login))) {
                    showActivity(getActivity(),LoginActivity.class);
                }else{
                    if (typeTitle != 0) {
                        Intent intenttt = new Intent(getActivity(), MainActivity.class);
                        intenttt.putExtra(Constants.NAVIGATION_FLAG , 1);
                        showActivity(getActivity(), intenttt);
                        getActivity().finish();
                    }
                    EventBus.getDefault().post(new CallMainPagerChangeEvent(1));
                }
                break;
            case R.id.iv_back_to_top:
                if (ptrLv != null) {
                    ListView refreshableView = ptrLv.getRefreshableView();
                    refreshableView.smoothScrollToPosition(0);
                }
                break;
            case R.id.title_right_iv:
                moreWindow = new CommonTitleRightWindow(getActivity());
                moreWindow.showWindow(title_right_iv);
                break;
        }
    }

    class MyOnScrollListener implements AbsListView.OnScrollListener {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem >=4) {
                iv_back_to_top.setVisibility(View.VISIBLE);
            }else{
                iv_back_to_top.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(SelectShoppingCarEvent event) {
        if ("ShoppingCart".equals(event.falg)) {
            if (LoginUtils.isLogin()) {
                if (mAppList.size()==0) {
                    hideView();
                    startOverallLoading(ptrLv);//第一次加载动画
                }
                showLoadingDialog(getString(R.string.common_loading));
                getNet();
            }
        }else{
            falseone = true;
        }
    }

    private String couponType = "";
    private String vouchersID;//优惠券Id
    private String vouchersCode;//优惠码
    private String couponPrice = "";//优惠券价
    public Double couponDiscountPrice = 0.0;//折扣率
    public int tempCoup = 0;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //数据复位
        couponType = "";
        vouchersID = "";//优惠券Id
        vouchersCode = "";//优惠码
        couponPrice = "";//优惠券价
        couponDiscountPrice = 0.0;//折扣率
        tempCoup = 0;
        hasSelectCouponNum = "";

        if (requestCode < 0) {
            return;
        }
        if (data != null) {
            Log.d("cy", "data:" + data);
            datas = (HashMap<String, String>) data.getExtras().getSerializable(PRICE_COUPON);
            if (datas != null) {
//                int	type:类型（1 使用优惠券 2 使用优惠码）
//                long	ID: （优惠券/优惠码ID）
//                string	activityType:优惠码类型（1代金券，2折扣）
//                string	discountAmount:优惠金额
//                string	discount:折扣率
                couponType = datas.get("type");
                if (couponType.equals("1")) {
                    rb_price_tv.setText(String.format(getString(R.string.my_coupon_unit_des), new DecimalFormat("#").format(Double.parseDouble(datas.get("discountAmount")))));
                    vouchersID = datas.get("ID");
                } else {
                    vouchersCode = data.getStringExtra(INPUT_COUPON_CODE);
                    if (datas.get("activityType").equals("1")) {
                        //代金券
                        rb_price_tv.setText(String.format(getString(R.string.my_coupon_unit_des), new DecimalFormat("#").format(Double.parseDouble(datas.get("discountAmount")))));
                    } else {
                        //折扣
                        rb_price_tv.setText(String.format(getString(R.string.common_discount_des), new DecimalFormat("#").format(Double.parseDouble(datas.get("discount")) * 100)));
                        couponDiscountPrice = (Double.parseDouble(datas.get("discount")));
                    }
                }
                if (couponDiscountPrice == 0) {
                    couponPrice = new DecimalFormat("#").format(Double.parseDouble(datas.get("discountAmount")));
                    hasSelectCouponNum = datas.get("discountAmount");
                    if (!TextUtils.isEmpty(couponPrice))
                        tempCoup = Integer.parseInt(couponPrice);
                }
            }
            if (tempCoup > 0 && tempCoup >= priea) {
                showToastMsg(getString(R.string.submit_order_bonus_tips));
            }
            setView();
        }
    }

    private void setToatalPrice(float tempCurrenTotal) {

        //优惠券情况
        if (tempCoup > 0) {
            /**总价<优惠券*/
            if (tempCoup >= tempCurrenTotal) {
                shopping_cart_total_prices.setText(getString(R.string.common_taishu_unit) + MoneyShowTool.twolastDF(tempCurrenTotal, true));
//                showToastMsg(getString(R.string.submit_order_bonus_tips));
                has_select_coupon_loy.setVisibility(View.GONE);
                has_select_you_loy.setVisibility(View.VISIBLE);
                //复位数据
                couponType = "";
                vouchersID = "";//优惠券Id
                vouchersCode = "";//优惠码
                couponPrice = "";//优惠券价
                hasSelectCouponNum = "";
                couponDiscountPrice = 0.0;//折扣率
                tempCoup = 0;
            } else {
                has_select_coupon_loy.setVisibility(View.VISIBLE);
                has_select_you_loy.setVisibility(View.GONE);
                shopping_cart_total_prices.setText(getString(R.string.common_taishu_unit) + MoneyShowTool.twolastDF((tempCurrenTotal - tempCoup),true));
            }
            return;
        }
        //折扣率情况
        if (couponDiscountPrice > 0 && couponDiscountPrice < 1) {
            //折扣率，前提couponPrice="";
            has_select_coupon_loy.setVisibility(View.VISIBLE);
            has_select_you_loy.setVisibility(View.GONE);
            shopping_cart_total_prices.setText(getString(R.string.common_taishu_unit) + MoneyShowTool.twolastDF(couponDiscountPrice * tempCurrenTotal));
            return;
        }
        has_select_coupon_loy.setVisibility(View.GONE);
        has_select_you_loy.setVisibility(View.VISIBLE);
        shopping_cart_total_prices.setText(getString(R.string.common_taishu_unit) + MoneyShowTool.twolastDF(tempCurrenTotal));
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
//        showToastMsg("下拉");
        CURRENT_PAGE = 1;
        getSoppingCart();
        showLoadingDialog(getString(R.string.common_loading));
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
//        showToastMsg("上拉");
        getSoppingCart();
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    //界面UI修改，数据计算
    private void setView(){
        priea = 0f;
        for (int i = 0; i < mAppList.size(); i++) {
            if (mAppList.get(i).IsSelected == 1 && mAppList.get(i).Status != 5) {
                priea += mAppList.get(i).HKPrice * mAppList.get(i).Quantity;
            }
        }
        if (priea >= 99) {
            shopping_cart_marked.setVisibility(View.GONE);
            shopping_cart_discount.setVisibility(View.VISIBLE);
        }else{
            //复位数据
            couponType = "";
            vouchersID = "";//优惠券Id
            vouchersCode = "";//优惠码
            couponPrice = "";//优惠券价
            couponDiscountPrice = 0.0;//折扣率
            tempCoup = 0;
            hasSelectCouponNum = "";
            shopping_cart_marked.setVisibility(View.VISIBLE);
            shopping_cart_discount.setVisibility(View.GONE);
        }
        setToatalPrice(priea);
    }

    //获取购物车
    private void getSoppingCart() {

        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("type", 1);//1.购物车列表，2.购物车数量
        ajaxParams.putCommonTypeParam("userID", GlobalApp.getInstance().getUserId());
//        ajaxParams.putCommonTypeParam("pageNo", CURRENT_PAGE);
//        ajaxParams.putCommonTypeParam("pageSize", PAGE_SIZE);

        FormResultRequest<ArrayList<SoppingCartBean>> getResultRequest = new FormResultRequest<ArrayList<SoppingCartBean>>(
                UrlConstants.MYSHOPPINGCART, getsSoppingCartReq, this,
                new TypeToken<RequestResult<ArrayList<SoppingCartBean>>>() {
                }.getType());
        getResultRequest.setRequestParams(ajaxParams);
        executeRequest(getResultRequest);

    }

    private Response.Listener<RequestResult<ArrayList<SoppingCartBean>>> getsSoppingCartReq = new Response.Listener<RequestResult<ArrayList<SoppingCartBean>>>() {

        @Override
        public void onResponse(RequestResult<ArrayList<SoppingCartBean>> result) {
            stopOverallLoading();
            hideView();
            mAppList.clear();
            if (result.getRs() != null && result.getRs().size() > 0) {
                if (result.getRs().size()<10) {
                    ptrLv.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
                }else{
                    ptrLv.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
                }
                if (CURRENT_PAGE == 1) {
                    mAppList.clear();
                }
//                CURRENT_PAGE ++;
                mAppList.addAll(result.getRs());
                mAdapter.setGroup(mAppList);
            }else if (mAppList.size() == 0){
                ptrLv.setMode(PullToRefreshBase.Mode.DISABLED);
                shoppingView();
            }else{
                ptrLv.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
            }
            dismissLoadingDialog();
            ptrLv.onRefreshComplete();
            getboolSelect();
            setView();
        }
    };

    @Override
    public void onErrorResponse(VolleyError error) {
        super.onErrorResponse(error);
        ptrLv.onRefreshComplete();
        if (mAppList.size() == 0) {
            showNoNetView(ptrLv, reloaClickListener );
        }
    }

    private View.OnClickListener reloaClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // 第一页就加载失败了
            hideOverLay();
            hideNoNetView();
            startOverallLoading(ptrLv);
            getSoppingCart();
        }
    };

    //是否勾选
    public void getSelectGoods(boolean isSelected , String ShoppingCartId) {
        showLoadingDialog(getString(R.string.common_loading));
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", GlobalApp.getInstance().getUserId());
        ajaxParams.putCommonTypeParam("isSelected", isSelected);
        ajaxParams.putCommonTypeParam("ShoppingCartId", ShoppingCartId);

        FormResultRequest<SelectGoods> getResultRequest = new FormResultRequest<SelectGoods>(
                UrlConstants.SELECTGOODS, getsSelectGoodsReq, this,
                new TypeToken<RequestResult<SelectGoods>>() {
                }.getType());
        getResultRequest.setRequestParams(ajaxParams);
        executeRequest(getResultRequest);

    }

    private Response.Listener<RequestResult<SelectGoods>> getsSelectGoodsReq = new Response.Listener<RequestResult<SelectGoods>>() {

        @Override
        public void onResponse(RequestResult<SelectGoods> result) {
            if (result != null && result.flag == 1 && result.getRs().ShoppingCartId.length() > 0) {
                String [] CartIds = result.getRs().ShoppingCartId.split(",");
                for (int i = 0; i < CartIds.length; i++) {
                    w:for (int j = 0; j < mAppList.size(); j++) {
                        if (mAppList.get(j).ShoppingCartId == Long.parseLong(CartIds[i])) {
                            if (result.getRs().isSelected) {
                                mAppList.get(j).IsSelected = 1;
                            }else{
                                mAppList.get(j).IsSelected = 0;
                            }
                            break w;
                        }
                    }
                }
                setView();
                mAdapter.setGroup(mAppList);
                getboolSelect();
            }else{
            showToastMsg(result.msg);
            }
            dismissLoadingDialog();
        }
    };

    //判断是否已经全选
    private boolean getboolSelect(){
        for (int i = 0; i < mAppList.size(); i++) {
            if (mAppList.get(i).Status != 5  && mAppList.get(i).IsSelected == 0) {
                shopping_cart_all.setImageResource(R.mipmap.common_unselect_choise_icon);
                return false;
            }
        }
        shopping_cart_all.setImageResource(R.mipmap.common_select_choise_icon);
        return true;
    }

    //数量改变
    public void getNumberData(int Quantity , long SkuProductId) {
        showLoadingDialog(getString(R.string.common_loading));
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", GlobalApp.getInstance().getUserId());
        ajaxParams.putCommonTypeParam("LanguageID", getString(R.string.common_lang));
        ajaxParams.putCommonTypeParam("Quantity", Quantity);//商品数量
        ajaxParams.putCommonTypeParam("SkuProductId", SkuProductId);
        ajaxParams.putCommonTypeParam("IsRewrite", true);

        FormResultRequest<SelectGoods> getResultRequest = new FormResultRequest<SelectGoods>(
                UrlConstants.UPDATECARTPRODUCT, getsNumberDataReq, this,
                new TypeToken<RequestResult<SelectGoods>>() {
                }.getType());
        getResultRequest.setRequestParams(ajaxParams);
        executeRequest(getResultRequest);

    }

    private Response.Listener<RequestResult<SelectGoods>> getsNumberDataReq = new Response.Listener<RequestResult<SelectGoods>>() {

        @Override
        public void onResponse(RequestResult<SelectGoods> result) {
            if (result != null && result.flag == 1) {
                for (int i = 0; i < mAppList.size(); i++) {
                    if (mAppList.get(i).SkuProductId == result.getRs().SKU_ProducId) {
                        mAppList.get(i).Quantity = result.getRs().Quantity;
                    }
                }
                setView();
                mAdapter.setGroup(mAppList);
            }else{
                showToastMsg(result.msg);
            }
            dismissLoadingDialog();
        }
    };

    //删除
    public void getDeleteData(long ShoppingCartId) {
        showLoadingDialog(getString(R.string.common_loading));
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", GlobalApp.getInstance().getUserId());
        ajaxParams.putCommonTypeParam("LanguageID", getString(R.string.common_lang));
        ajaxParams.putCommonTypeParam("ShoppingCartId", ShoppingCartId);

        FormResultRequest<Long> getResultRequest = new FormResultRequest<Long>(
                UrlConstants.DELETECARTPRODUCT, getsDeleteDataReq, this,
                new TypeToken<RequestResult<Long>>() {
                }.getType());
        getResultRequest.setRequestParams(ajaxParams);
        executeRequest(getResultRequest);

    }

    private Response.Listener<RequestResult<Long>> getsDeleteDataReq = new Response.Listener<RequestResult<Long>>() {

        @Override
        public void onResponse(RequestResult<Long> result) {
            if (result != null && result.flag == 1) {
                w: for (int i = 0; i < mAppList.size(); i++) {
                    if (mAppList.get(i).ShoppingCartId == result.getRs()) {
                        mAppList.remove(i);
                        break w;
                    }
                }
                EventBus.getDefault().post(new SelectShoppingCarEvent(""));
                if (mAppList.size() == 0) {
                        shoppingView();
                }
                setView();
                mAdapter.setGroup(mAppList);
            }else{
                showToastMsg(result.msg);
            }
            dismissLoadingDialog();
        }
    };

    //当金额小于优惠券的时候提示框
    public  void GetViewDialog (int dialogint , final String types  ,final boolean falsefalg ,final String ShoppingCartId ,final int Quantity ,final long SkuProductId){
        final PromptOperatDialog dialog = new PromptOperatDialog(getActivity(), null);
        if (dialogint == 1) {
            dialog.setTitile(getString(R.string.shopping_show_dialog));//价格低于99的提示
        }else{
            dialog.setTitile(getString(R.string.submit_order_bonus_tips));//优惠券大于等于金额的提示
        }
        dialog.setcancel(getString(R.string.left_cancle));
        dialog.setconfirm(getString(R.string.use_bonus_confirm));
        dialog.setCancelClick(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setConfirmClick(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (types.equals("1")) {
                    getSelectGoods(falsefalg, ShoppingCartId);
                } else if (types.equals("2")) {
                    getNumberData(Quantity, SkuProductId);
                } else if (types.equals("3")) {
                    getDeleteData(Long.parseLong(ShoppingCartId));
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    //提交订单
    public void getSubmitOrder () {
        String shoppingStr = "";
        for (int i = 0; i < mAppList.size(); i++) {
            if (mAppList.get(i).IsSelected == 1 && mAppList.get(i).Status != 5) {
                shoppingStr = "TEST";
            }
        }
        if (StringUtils.isEmpty(shoppingStr)) {
            showToastMsg(getString(R.string.shopping_null));
            return;
        }

        showLoadingDialog(getString(R.string.common_loading));
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", GlobalApp.getInstance().getUserId());
//        if (!StringUtils.isEmail(vouchersID)) {
        ajaxParams.putCommonTypeParam("vouchersID", vouchersID);
//        }else if(!StringUtils.isEmail(vouchersCode)){
        ajaxParams.putCommonTypeParam("discountCode", vouchersCode);
//        }

        FormResultRequest<SubmitBean> getResultRequest = new FormResultRequest<SubmitBean>(
                UrlConstants.SUBMITORDER, getsSubmitOrderReq, this,
                new TypeToken<RequestResult<SubmitBean>>() {
                }.getType());
        getResultRequest.setRequestParams(ajaxParams);
        executeRequest(getResultRequest);

    }

    private Response.Listener<RequestResult<SubmitBean>> getsSubmitOrderReq = new Response.Listener<RequestResult<SubmitBean>>() {

        @Override
        public void onResponse(RequestResult<SubmitBean> result) {
            if (result != null && result.flag == 1) {
                EventBus.getDefault().post(new SelectShoppingCarEvent(""));
                SubmitBean rs = result.getRs();
                Intent intent = new Intent(getActivity(), ChoosePayModesActivity.class);
                intent.putExtra(BasePay.PAYORDERNO, rs.getPaymentOrderId());
                intent.putExtra(BasePay.PHONE, GlobalApp.getInstance().getUserModel().getPhone());
                intent.putExtra(BasePay.USERID, GlobalApp.getInstance().getUserId());
                showActivity(getActivity(), intent);
            }
            dismissLoadingDialog();
        }
    };

}
